import * as iotTokenTool from "./tool/iotTokenTool";
import * as iotDeviceTool from "./tool/iotDeviceTool";
import * as iotDeviceSettingTool from "./tool/iotDeviceSettingTool";
import * as iotDeviceSensorTool from "./tool/iotDeviceSensorTool";
import * as iotDeviceSensorDataTool from "./tool/iotDeviceSensorDataTool";
import * as httpTool from './util/httpTool';
import TokenGetReq from "./req/TokenGetReq";
import DeviceListReq from "./req/DeviceListReq";
import DeviceBatchStatusReq from "./req/DeviceBatchStatusReq";
import DeviceGetReq from "./req/DeviceGetReq";
import DeviceSensorDataListReq from "./req/DeviceSensorDataListReq";
import DeviceSettingListReq from "./req/DeviceSettingListReq";
import DeviceSensorListWithReq from "./req/DeviceSensorListWithReq";
import DeviceSettingSendReq from "./req/DeviceSettingSendReq";
refreshToken();
// function setToken(){
//     httpTool.setToken(${后台生成永久token})
//     listDevice()
// }
function refreshToken() {
    const req = new TokenGetReq("${apiKey}", "${apiSecret}");
    iotTokenTool.refresh(req).then(data => {
        if (!data) {
            console.error('iotTokenTool.refresh data == null');
            return;
        }
        httpTool.setToken(data.accessToken);
        listDevice();
    });
}
function listDevice() {
    const req = new DeviceListReq();
    req.status = "ONLINE";
    iotDeviceTool.list(req).then(data => {
        if (!data) {
            console.error('iotDeviceTool.list data == null');
            return;
        }
        data.forEach(item => {
            console.log(item.deviceCore);
        });
        const deviceCoreList = [];
        for (let i = 0, j = data.length; i < j; i++) {
            const device = data[i];
            if (i === 0) {
                getDevice(device.deviceCore);
            }
            else if (i < 5) {
                deviceCoreList.push(device.deviceCore);
            }
        }
        batchDeviceStatus(deviceCoreList);
    });
}
function batchDeviceStatus(deviceCoreList) {
    const req = new DeviceBatchStatusReq();
    req.deviceCoreList = deviceCoreList;
    iotDeviceTool.batchDeviceStatus(req).then(data => {
        if (!data) {
            console.error('iotDeviceTool.batchDeviceStatus data == null');
            return;
        }
        data.forEach(item => {
            console.log(item.deviceCore, item.status);
        });
    });
}
function getDevice(deviceCore) {
    const req = new DeviceGetReq();
    req.deviceCore = deviceCore;
    iotDeviceTool.get(req).then(data => {
        if (!data) {
            console.error('iotDeviceTool.get data == null');
            return;
        }
        console.log(data.deviceCore);
        listSensor(data.deviceCore);
        listSetting(data.deviceCore);
        sendSetting(data.deviceCore);
    });
}
function listSensor(deviceCore) {
    const req = new DeviceSensorListWithReq();
    req.deviceCore = deviceCore;
    req.withParam = true;
    req.withSetting = true;
    req.withExplain = true;
    iotDeviceSensorTool.list(req).then(data => {
        if (!data) {
            console.error('iotDeviceSensorTool.list data == null');
            return;
        }
        data.forEach((sensor) => {
            console.log(sensor.port);
            if (sensor.explainList) {
                sensor.explainList.forEach(item => {
                });
            }
            if (sensor.paramList) {
                sensor.paramList.forEach(item => {
                });
            }
            if (sensor.settingList) {
                sensor.settingList.forEach(item => {
                });
            }
        });
    });
}
function listSensorDataWithExplain(deviceCore, port) {
    const endTime = new Date().getTime() / 1000;
    const startTime = endTime - (60 * 60 * 12);
    const req = new DeviceSensorDataListReq();
    req.deviceCore = deviceCore;
    req.port = port;
    req.enableEval = true;
    req.endTime = endTime;
    iotDeviceSensorDataTool.listWithExplain(req).then(data => {
        if (!data) {
            console.error('iotDeviceSensorDataTool.listWithExplain data == null');
            return;
        }
        data.explainList.forEach(() => {
        });
        data.dataList.forEach(() => {
        });
    });
}
function listSetting(deviceCore) {
    const req = new DeviceSettingListReq();
    req.deviceCore = deviceCore;
    iotDeviceSettingTool.list(req).then(data => {
        if (!data) {
            console.error('iotDeviceSettingTool.list data == null');
            return;
        }
        data.forEach(item => {
            console.log(item.keyName);
        });
    });
}
function sendSetting(deviceCore) {
    const req = new DeviceSettingSendReq();
    req.deviceCore = deviceCore;
    req.keyName = 'getData';
    req.port = '0';
    req.value = 'null';
    iotDeviceSettingTool.send(req).then(data => {
        if (!data) {
            console.error('iotDeviceSettingTool.send data == null');
            return;
        }
    });
}
